package Menus;

import java.awt.Color;
import java.awt.Graphics;
import Constructions.Construction;
import FrameWork.Menu;
import Main.SpaceGame;
import Power.Generator;
import Power.PowerNode;

public class BuildingMenu extends Menu
{
	boolean cursorRequirement = true;
	int level = 1;
	int pixXPos = SpaceGame.windowXSize-200;
	int pixYPos = 100;
	int width = 200;
	int height = SpaceGame.windowYSize-100;
	private SpaceGame main;
	Construction nearestBuilding = null;
	double nearestBuildingDistance = -1;
	
	public BuildingMenu(SpaceGame main)
	{
		this.main = main;
	}

	@Override
	public void draw(Graphics g) 
	{
		updateConstruction();
		g.translate(pixXPos, pixYPos);
		
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.DARK_GRAY);
		g.fillRect(5, 5, width-10, height-10);
		
		if (nearestBuilding != null)
		{
			printMultiColorLines(g, nearestBuilding.text, nearestBuilding.colors, nearestBuilding.textColorIndex);
		}
		
		g.translate(-pixXPos, -pixYPos);
	}

	@Override
	public boolean requiresCursor() 
	{
		return cursorRequirement;
	}

	@Override
	public void setRequiresCursor(boolean f) 
	{
		this.cursorRequirement = f;
	}

	@Override
	public int getLevel() 
	{
		return level;
	}
	
	private void updateConstruction()
	{
		nearestBuildingDistance = -1;
		for (Construction c:main.constructionManager.constructionList)
		{
			c.setFlashing(false);
			int xDistance = Math.abs(c.getX()-main.cursor.getX());
			int yDistance = Math.abs(c.getY()-main.cursor.getY());
			double distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
			if (distance < nearestBuildingDistance || nearestBuildingDistance == -1)
			{
				nearestBuildingDistance = distance;
				nearestBuilding = c;
			}
		}
		for(PowerNode p:main.powerManager.nodeList)
		{
			p.setFlashing(false);
			int xDistance = Math.abs(p.getX()-main.cursor.getX());
			int yDistance = Math.abs(p.getY()-main.cursor.getY());
			double distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
			if (distance < nearestBuildingDistance || nearestBuildingDistance == -1)
			{
				nearestBuildingDistance = distance;
				nearestBuilding = p;
			}
		}
		for(Generator g:main.powerManager.generatorList)
		{
			g.setFlashing(false);
			int xDistance = Math.abs(g.getX()-main.cursor.getX());
			int yDistance = Math.abs(g.getY()-main.cursor.getY());
			double distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
			if (distance < nearestBuildingDistance || nearestBuildingDistance == -1)
			{
				nearestBuildingDistance = distance;
				nearestBuilding = g;
			}
		}
		nearestBuilding.setFlashing(true);
	}
	
	public Construction getNearestBuilding()
	{
		return nearestBuilding;
	}
}
